package applePay

//https://developer.apple.com/documentation/appstorereceipts/requestbody
//Content-Type: application/json
//POST https://buy.itunes.apple.com/verifyReceipt
type AppPayRequestBody struct {
	ReceiptData            string `json:"receipt-data,omitempty"` //(be required)
	Password               string `json:"password,omitempty"`     //(be required)
	ExcludeOldTransactions bool   `json:"exclude-old-transactions,omitempty"`
}

//https://developer.apple.com/documentation/appstorereceipts/responsebody/latest_receipt_info
type TLastestReceiptInfo struct {
	CancellationDate        string `json:"cancellation_date,omitempty"`
	CancellationDateMs      string `json:"cancellation_date_ms,omitempty"`
	CancellationDatePst     string `json:"cancellation_date_pst,omitempty"`
	CancellationReason      string `json:"cancellation_reason,omitempty"`
	ExpiresDate             string `json:"expires_date,omitempty"`
	ExpiresDateMs           string `json:"expires_date_ms,omitempty"`
	ExpiresDatePst          string `json:"expires_date_pst,omitempty"`
	IsInIntroOfferPeriod    string `json:"is_in_intro_offer_period,omitempty"`
	IsTrialPeriod           string `json:"is_trial_period,omitempty"`
	OriginalPurchaseDate    string `json:"original_purchase_date,omitempty"`
	OriginalPurchaseDateMs  string `json:"original_purchase_date_ms,omitempty"`
	OriginalPurchaseDatePst string `json:"original_purchase_date_pst,omitempty"`
	OriginalTransactionID   string `json:"original_transaction_id,omitempty"`
	ProductID               string `json:"product_id,omitempty"`
	PromotionalOfferID      string `json:"promotional_offer_id,omitempty"`
	PurchaseDate            string `json:"purchase_date,omitempty"`
	PurchaseDateMs          string `json:"purchase_date_ms,omitempty"`
	PurchaseDatePst         string `json:"purchase_date_pst,omitempty"`
	Quantity                string `json:"quantity,omitempty"`
	TransactionID           string `json:"transaction_id,omitempty"`
	WebOrderLineItemID      string `json:"web_order_line_item_id,omitempty"`

	IsUpgraded                  string `json:"is_upgraded,omitempty"`
	OfferCodeRefName            string `json:"offer_code_ref_name,omitempty"`
	SubscriptionGroupIdentifier string `json:"subscription_group_identifier,omitempty"`
}

//https://developer.apple.com/documentation/appstorereceipts/responsebody/pending_renewal_info
type TPendingRenewalInfo struct {
	AutoRenewProductID        string `json:"auto_renew_product_id,omitempty"`
	AutoRenewStatus           string `json:"auto_renew_status,omitempty"`
	ExpirationIntent          string `json:"expiration_intent,omitempty"`
	GracePeriodExpiresDate    string `json:"grace_period_expires_date,omitempty"`
	GracePeriodExpiresDateMs  string `json:"grace_period_expires_date_ms,omitempty"`
	GracePeriodExpiresDatePst string `json:"grace_period_expires_date_pst,omitempty"`
	IsInBillingRetryPeriod    string `json:"is_in_billing_retry_period,omitempty"`
	OfferCodeRefName          string `json:"offer_code_ref_name,omitempty"`
	OriginalTransactionID     string `json:"original_transaction_id,omitempty"`
	PriceConsentStatus        string `json:"price_consent_status,omitempty"`
	ProductID                 string `json:"product_id,omitempty"`
}

//https://developer.apple.com/documentation/appstorereceipts/responsebody/receipt/in_app
type TInApp struct {
	CancellationDate        string `json:"cancellation_date,omitempty"`
	CancellationDateMs      string `json:"cancellation_date_ms,omitempty"`
	CancellationDatePst     string `json:"cancellation_date_pst,omitempty"`
	CancellationReason      string `json:"cancellation_reason,omitempty"`
	ExpiresDate             string `json:"expires_date,omitempty"`
	ExpiresDateMs           string `json:"expires_date_ms,omitempty"`
	ExpiresDatePst          string `json:"expires_date_pst,omitempty"`
	IsInIntroOfferPeriod    string `json:"is_in_intro_offer_period,omitempty"`
	IsTrialPeriod           string `json:"is_trial_period,omitempty"`
	OriginalPurchaseDate    string `json:"original_purchase_date,omitempty"`
	OriginalPurchaseDateMs  string `json:"original_purchase_date_ms,omitempty"`
	OriginalPurchaseDatePst string `json:"original_purchase_date_pst,omitempty"`
	OriginalTransactionID   string `json:"original_transaction_id,omitempty"`
	ProductID               string `json:"product_id,omitempty"`
	PromotionalOfferID      string `json:"promotional_offer_id,omitempty"`
	PurchaseDate            string `json:"purchase_date,omitempty"`
	PurchaseDateMs          string `json:"purchase_date_ms,omitempty"`
	PurchaseDatePst         string `json:"purchase_date_pst,omitempty"`
	Quantity                string `json:"quantity,omitempty"`
	TransactionID           string `json:"transaction_id,omitempty"`
	WebOrderLineItemID      string `json:"web_order_line_item_id,omitempty"`
}

//https://developer.apple.com/documentation/appstorereceipts/responsebody/receipt
type TReceipt struct {
	AdamID                     int64     `json:"adam_id,omitempty"`
	AppItemID                  int64     `json:"app_item_id,omitempty"`
	ApplicationVersion         string    `json:"application_version,omitempty"`
	BundleID                   string    `json:"bundle_id,omitempty"`
	DownloadID                 int64     `json:"download_id,omitempty"`
	ExpirationDate             string    `json:"expiration_date,omitempty"`
	ExpirationDateMs           string    `json:"expiration_date_ms,omitempty"`
	ExpirationDatePst          string    `json:"expiration_date_pst,omitempty"`
	InApp                      []*TInApp `json:"in_app,omitempty"`
	OriginalApplicationVersion string    `json:"original_application_version,omitempty"`
	OriginalPurchaseDate       string    `json:"original_purchase_date,omitempty"`
	OriginalPurchaseDateMs     string    `json:"original_purchase_date_ms,omitempty"`
	OriginalPurchaseDatePst    string    `json:"original_purchase_date_pst,omitempty"`
	PreorderDate               string    `json:"preorder_date,omitempty"`
	PreorderDateMs             string    `json:"preorder_date_ms,omitempty"`
	PreorderDatePst            string    `json:"preorder_date_pst,omitempty"`
	ReceiptCreationDate        string    `json:"receipt_creation_date,omitempty"`
	ReceiptCreationDateMs      string    `json:"receipt_creation_date_ms,omitempty"`
	ReceiptCreationDatePst     string    `json:"receipt_creation_date_pst,omitempty"`
	ReceiptType                string    `json:"receipt_type,omitempty"`
	RequestDate                string    `json:"request_date,omitempty"`
	RequestDateMs              string    `json:"request_date_ms,omitempty"`
	RequestDatePst             string    `json:"request_date_pst,omitempty"`
	VersionExternalIdentifier  int64     `json:"version_external_identifier,omitempty"`
}

//https://developer.apple.com/documentation/appstorereceipts/responsebody
type AppPayResponseBody struct {
	Environment        string              `json:"environment,omitempty"`
	IsRetryable        bool                `json:"is-retryable,omitempty"`
	LatestReceipt      byte                `json:"latest_receipt,omitempty"`
	LastestReceiptInfo TLastestReceiptInfo `json:"latest_receipt_info,omitempty"`
	PendingRenewalInfo TPendingRenewalInfo `json:"pending_renewal_info,omitempty"`
	Receipt            TReceipt            `json:"receipt,omitempty"`
	Status             int                 `json:"status,omitempty"` //https://developer.apple.com/documentation/appstorereceipts/status
}

var (
	//response some status.
	RespBodyStatus = map[int]string{
		0:     "success",
		21000: "The request to the App Store was not made using the HTTP POST request method.",
		21001: "This status code is no longer sent by the App Store.",
		21002: "The data in the receipt-data property was malformed or the service experienced a temporary issue. Try again.",
		21003: "The receipt could not be authenticated.",
		21004: "The shared secret you provided does not match the shared secret on file for your account.",
		21005: "The receipt server was temporarily unable to provide the receipt. Try again.",
		21006: "This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response. Only returned for iOS 6-style transaction receipts for auto-renewable subscriptions.",
		21007: "This receipt is from the test environment, but it was sent to the production environment for verification.",
		21008: "This receipt is from the production environment, but it was sent to the test environment for verification.",
		21009: "Internal data access error. Try again later.",
		21010: "The user account cannot be found or has been deleted.",
	}

	//Status codes 21100-21199 are various internal data access errors.
	AccessErrors_Min = 21100
	AccessErrors_Max = 21199

	// POST some url.
	CommonUrl  = "https://buy.itunes.apple.com/verifyReceipt"
	SandboxUrl = "https://sandbox.itunes.apple.com/verifyReceipt"

	//
	ContentType = "application/json"
)

//response error codes
const (
	SUCCESS                  = "success"
	REQUST_DATA_MARSHAL_FAIL = "request data marshal fail."
	POST_DATA_FAIL           = "post data fail."
	READ_RESPONSE_FAIL       = "read response fail."
	UNMARSHAL_RESPONSE_FAIL  = "unmarshal response fail."
	ACCESS_ERRORS            = "Internal data access errors."
)
